package com.wing.productionPlan.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionPlan;
import com.wing.productionPlan.model.response.ProductionPlanVO;
import com.wing.productionPlan.model.response.ProductionWorkOrderVO;
import com.wing.productionPlan.service.ProductionPlanService;
import com.wing.productionPlan.service.ProductionWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "Admin-生产执行（按计划）", tags = "#/productionPlan/productionAsPlanned")
@RestController("productionAsPlannedController")
@RequestMapping("productionPlan/admin/productionAsPlanned")
@RequiredArgsConstructor
public class ProductionAsPlannedController {
    private final ProductionPlanService productionPlanService;


    @ApiOperation(value = "查询所有生产执行（按计划）", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductionPlanVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String code
            ,String skuCode
            ,String skuName
            ,String startCreateTime
            ,String endCreateTime
            ,String status
    ){

        Page<ProductionPlan> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        //搜索
        if(StringUtils.isNotBlank(code)) {
            paramMap.put("code", code);
        }
        if(StringUtils.isNotBlank(skuCode)) {
            paramMap.put("customerSkuCode", skuCode);
        }
        if(StringUtils.isNotBlank(skuName)) {
            paramMap.put("customerSkuName", skuName);
        }
        if(StringUtils.isNotBlank(startCreateTime)){
            paramMap.put("startCreateTime", startCreateTime);
        }
        if(StringUtils.isNotBlank(endCreateTime)){
            paramMap.put("endCreateTime", endCreateTime);
        }
        if(StringUtils.isNotBlank(status)) {
            Integer value = ProductionPlan.Status.valueOf(status).getValue();
            paramMap.put("status", value);
        }

        IPage<ProductionPlanVO> list = productionPlanService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

}
